//
//  SettingViewController.m
//  QuicklyPlay
//
//  Created by 你的温柔是我唯一沉溺 on 15/10/31.
//  Copyright © 2015年 OPteam. All rights reserved.
//

#import "SettingViewController.h"
#import "FeedBackViewController.h"
#import "AboutUsViewController.h"
#import "DeclarationViewController.h"

@interface SettingViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UITableView *tableView;

@property(strong, nonatomic)UILabel * praiseLabel ;

@end

@implementation SettingViewController




- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.dataSource = self ;
    self.tableView.delegate = self ;
    
    [[GestureRecognizerTool creatScreenEdgePanGestureRecognizerLeftWithViewController:self] addTarget:self action:@selector(BarItemBack:)];

}








- (IBAction)BarItemBack:(UIBarButtonItem *)sender {
    
    [self dismissViewControllerAnimated:NO completion:nil];
    
}





//tableView 需要实现的方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4 ;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:@"flag" forIndexPath:indexPath] ;
    if (indexPath.row == 0) {
        cell.textLabel.text = @"清空图片缓存";
    } else if (indexPath.row == 1) {
        cell.textLabel.text = @"意见反馈";
    } else if (indexPath.row == 2) {
        cell.textLabel.text = @"免责声明";
    } else if (indexPath.row == 3) {
        cell.textLabel.text =@"给我们一个好评吧~";
        
        //self.praiseLabel = [[UILabel alloc]initWithFrame:CGRectMake(300, 9, 25, 25)] ;
        self.praiseLabel = [[UILabel alloc]init] ;
        
        self.praiseLabel.backgroundColor = [UIColor redColor] ;
        self.praiseLabel.text = @"1" ;
        self.praiseLabel.textColor = [UIColor whiteColor] ;
        self.praiseLabel.textAlignment = NSTextAlignmentCenter ;
        self.praiseLabel.layer.cornerRadius = 12.5 ;
        self.praiseLabel.layer.masksToBounds = YES;
        [cell.contentView addSubview:self.praiseLabel] ;
        
        [self.praiseLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(cell.contentView.mas_right) ;
            make.top.equalTo(cell.contentView.mas_top).offset(9) ;
            make.height.mas_equalTo(25) ;
            make.width.mas_equalTo(25) ;
        }] ;
        
        
        //创建tap手势
        UITapGestureRecognizer * tapGR = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGRAction:)] ;
        [self.praiseLabel addGestureRecognizer:tapGR] ;
        self.praiseLabel.userInteractionEnabled = YES ; //这句话必须加上

        
    }
    //给cell添加右箭头
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator ;
    //去掉cell的分割线
    tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (0 == indexPath.row){
        //NSLog(@"清空图片缓存") ;
        [[SDImageCache sharedImageCache] clearMemory] ;
        [[SDImageCache sharedImageCache] cleanDisk] ;
        [self clearCache] ;
        UIAlertView*alertV = [[UIAlertView alloc]initWithTitle:@"提示" message:@"清空缓存成功" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil ] ;
        [alertV show] ;
        
    }else if (1 == indexPath.row){
       FeedBackViewController * feedBackVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"FeedBackVC"] ;
        [self presentViewController:feedBackVC animated:YES completion:nil] ;
    }else if (2 == indexPath.row){
       DeclarationViewController*declarationVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"DeclarationVC"];
        [self presentViewController:declarationVC animated:YES completion:nil] ;
    }else if (3 == indexPath.row){
        
        NSLog(@"给我们一个好评吧~") ;
    }
}

#pragma mark 实现tap手势
-(void)tapGRAction:(UITapGestureRecognizer*)sender{
    
    NSLog(@"tap手势让红点消失") ;
    self.praiseLabel.hidden = YES ;
}



-(void)clearCache{
    
    dispatch_async(
                   dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
                   , ^{
                       NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
                       NSLog(@"%@", cachPath);
                       
                       NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
                      // NSLog(@"files :%ld",[files count]);
                       for (NSString *p in files) {
                           NSError *error;
                           NSString *path = [cachPath stringByAppendingPathComponent:p];
                           if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
                               [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
                           }
                       }
                       [self performSelectorOnMainThread:@selector(clearCacheSuccess) withObject:nil waitUntilDone:YES];
                   });
}
-(void)clearCacheSuccess{
    NSLog(@"清除成功") ;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}



@end
